package com.foreveredu.propms.web.struts.action;

import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.Role;
import com.foreveredu.propms.domain.User;
import com.foreveredu.propms.service.IManagementService;

public class AdminAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		User user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		Set<Role> myroles = user.getRoles();
		if (null != myroles) {
			String[] roleIds = new String[myroles.size()];
			int i = 0;
			for (Iterator<Role> iterator = myroles.iterator(); iterator
					.hasNext();) {
				Role role = (Role) iterator.next();
				roleIds[i] = Integer.toString(role.getId());
				i++;
				if (role.getName().equals("ADMIN")) {
					session.setAttribute("userRole", "ADMIN");
				}else{
					session.setAttribute("userRole", "CHARGEMAN");
					
				}
			}
		}
		session.setAttribute("user", user);
		
		return mapping.findForward("admin");
	}
	
}
